
/**
 * @constructor
 * 
 * @param{Canvas} canvas The JQuery canvas object
 * @param{float} updateTimeMs The update time interval 
 * @param{int} size
 * @param{hex} color
 */
function CellSprite(canvas, updateTimeMs, size, terrainType, menuManager, debugConsole) {
	
	$.extend(this,new Sprite(canvas, updateTimeMs, debugConsole));
	
	this.width = size;
	this.height = size;
	this.currentColor = CellSpriteColor.PASSABLE;	
	this.isMouseOver = false;
	this.gameState = GameState.AWAITING_START_NODE;
	this.terrainType = terrainType;
	this.menuManager = menuManager;
	this.cellSpritesContext;
	this.isPath;
	this.isPathStart;
	this.isPathEnd;
	this.isExploredNode;
	
	/**
	 * @public
	 */
	this.update = function() {
		var cellColor = this.currentColor;
		if(this.isPath) {
			cellColor = CellSpriteColor.PATH;
		}
		
		this.canvas.drawRect({
			  fillStyle: cellColor,
			  x: this.x,
			  y: this.y,
			  width: this.width,
			  height: this.height,
			  fromCenter:true
			});	
		
		if(this.isPathStart) {
			this.canvas.drawText({
				  strokeStyle: "#000",
				  strokeWidth: 1,
				  x: this.x,
				  y: this.y,
				  font: "10pt Verdana, sans-serif",
				  text: "S",
				  fromCenter:true
				});
		}
		
		if(this.isPathEnd) {
			this.canvas.drawText({
				  strokeStyle: "#000",
				  strokeWidth: 1,
				  x: this.x,
				  y: this.y,
				  font: "10pt Verdana, sans-serif",
				  text: "E",
				  fromCenter:true
				});
		}
		
		if(this.isExploredNode) {
			this.canvas.drawText({
				  strokeStyle: "#000",
				  strokeWidth: 1,
				  x: this.x,
				  y: this.y,
				  font: "10pt Verdana, sans-serif",
				  text: ".",
				  fillStyle:CellSpriteColor.PATH,
				  fromCenter:true
				});
		}
		
		if(this.isMouseOver) {
			this.canvas.drawRect({
				  strokeStyle: CellSpriteColor.HIGHLIGHTED,
				  strokeWidth:3,
				  x: this.x,
				  y: this.y,
				  width: this.width-3,
				  height: this.height-3,
				  fromCenter:true,
				  cornerRadius: 4
				});
		}
	};
	
	/**
	 * @public
	 */	
	this.getCellX = function() {
		return Math.floor(this.x/this.width);
	};
	
	/**
	 * @public
	 */	
	this.getCellY = function() {
		return Math.floor(this.y/this.height);
	};
	
	/**
	 * @public
	 * @returns {Boolean}
	 */
	this.getMouseOver = function() {
		return this.isMouseOver;
	};
	
	/**
	 * @public
	 * 
	 * @param x
	 * @param y
	 */
	this.onMouseDown = function(x,y) {
		switch(this.menuManager.getPlaceType()) {
		case PlaceType.PASSABLE_TERRAIN: {
			this.makePassableCell();
			break;
		}
		case PlaceType.IMPASSABLE_TERRAIN: {
			this.currentColor = CellSpriteColor.IMPASSABLE;
			break;
		}
		case PlaceType.SOMEWHAT_PASSABLE_TERRAIN: {
			this.currentColor = CellSpriteColor.SOMEWHAT_PASSABLE;
			break;
		}
		case PlaceType.PATH_START: {
			this.makeStartPathCell();
			break;
		}
		case PlaceType.PATH_END: {
			this.makeEndPathCell();
			break;
		}
		}
	};
	
	/**
	 * @public
	 */
	this.onMouseOver = function(x, y ,mouseDown) {
		this.isMouseOver = true;
		if(mouseDown) {
			this.onMouseDown(x, y);
		}
	};
	
	/**
	 * @public
	 */
	this.onMouseOut = function(x, y, mouseDown) {
		this.isMouseOver = false;
		if(mouseDown) {
			this.onMouseDown(x, y);
		}
	};
	
	this.onMouseMove = function(x,y,mouseDown) {
		if(mouseDown) {
			this.onMouseDown(x, y);
		}
	};
	
	/**
	 * @public
	 * @param gameState
	 */
	this.setGameState = function(gameState) {
		this.gameState = gameState;
	};
	

	/**
	 * @public
	 * @param terrainType
	 */
	this.edit = function(terrainType) {
		this.terrainType = terrainType;
	};
	
	/**
	 * @public
	 */
	this.makePassableCell = function() {
		this.currentColor = CellSpriteColor.PASSABLE;
	};
	
	/**
	 * @public
	 */
	this.makeStartPathCell = function() {
		this.cellSpritesContext.clearStartPathCell();
		this.isPath = true;
		this.isPathStart = true;
		this.cellSpritesContext.setStartPathCell(this);
	};
	
	/**
	 * @public
	 */
	this.makeEndPathCell = function() {
		this.cellSpritesContext.clearEndPathCell();
		this.isPath = true;
		this.isPathEnd = true;
		this.cellSpritesContext.setEndPathCell(this);
	};
	
	this.unmakeStartPathCell = function() {
		this.isPathStart = false;
		this.isPath = false;
	};
	
	this.unmakeEndPathCell = function() {
		this.isPathEnd = false;
		this.isPath = false;
	};
	
	this.unmakePathCell = function() {
		this.isPath = false;
	};
	
	this.makeInnerPathCell = function() {
		this.isPath = true;
	};
	
	this.makeExploredNodeCell = function() {
		this.isExploredNode = true;
	};
	
	this.unmakeExploredNodeCell = function() {
		this.isExploredNode = false;
	};
	
	this.setCellSpritesContext = function(cellSpritesContext) {
		this.cellSpritesContext = cellSpritesContext;
	};
	
	this.getCurrentColor = function() {
		return this.currentColor;
	};
	
}